/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibertest;


import hibernate.Phone;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Alexx
 */
public class DAOPersone {
    
    private List executeHQLQuery(String hql) {
        List resultList = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query q = session.createQuery(hql);
            resultList = q.list();
            session.getTransaction().commit();
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        return resultList;
    }
    

    public hibernate.Adres getAdrById(String id)  {         
        Session session = HibernateUtil.getSessionFactory().openSession();
        hibernate.Adres adr = (hibernate.Adres) session.load(hibernate.Adres.class, id);
        return adr;
    }
    
    public hibernate.Phone getTelById(String id)  {         
        Session session = HibernateUtil.getSessionFactory().openSession();
        hibernate.Phone adr = (hibernate.Phone) session.load(hibernate.Phone.class, id);
        return adr;
    }
    
    public hibernate.Persone getPersoneById(String id)  {         
        Session session = HibernateUtil.getSessionFactory().openSession();
        hibernate.Persone adr = (hibernate.Persone) session.load(hibernate.Persone.class, id);
        return adr;
    }
    
    public Collection getAllPersone()  {
      List persons = new ArrayList<hibernate.Persone>();
      Session session = HibernateUtil.getSessionFactory().openSession();
      persons = session.createCriteria(hibernate.Persone.class).list();
    
    return persons;
    }
    
    public void addPersone(hibernate.Persone persone) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(persone);
        session.getTransaction().commit();
    }

    public void updatePersone(hibernate.Persone persone) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.saveOrUpdate(persone);
        session.getTransaction().commit();
    }
    
    public void deletePersone(hibernate.Persone persone) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(persone);
        session.getTransaction().commit();
    }
    
    public hibernate.User getUserById(String id)  {         
        Session session = HibernateUtil.getSessionFactory().openSession();
        hibernate.User adr = (hibernate.User) session.load(hibernate.User.class, id);
        return adr;
    }
    
    public hibernate.User getUserByLogin(String login)  {         
        hibernate.User adr = (hibernate.User)executeHQLQuery("FROM User WHERE LOGIN='" + login + "'").get(0);
        return adr;
    }
    
    public void addTypePhone(hibernate.Teltype teltype) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(teltype);
        session.getTransaction().commit();
    }


}
